package com.nintendo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nintendo.dto.CategoryDTO;
import com.nintendo.entity.Category;
import com.nintendo.entity.Ebook;
import com.nintendo.mapper.CategoryMapper;
import com.nintendo.service.CategoryService;
import com.nintendo.util.SnowFlake;
import com.nintendo.vo.PageInfoVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SnowFlake snowFlake;
    @Override
    public PageInfoVo findAllByNameAuthPage(CategoryDTO categoryDTO) {
        PageHelper.startPage(categoryDTO.getPageNum(),1000);
        List<Category> allByNameAuthPage = categoryMapper.findAllByNameAuthPage(categoryDTO);
        PageInfo<Category> pageInfo = new PageInfo<>(allByNameAuthPage);
        return new PageInfoVo(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void saveCategory(Category category) {
        if(category.getId()!=0){
            categoryMapper.updateCategoryById(category);
        }else{
            Long nextId = snowFlake.nextId();
            String nextIdS = nextId.toString();
            category.setId(Long.valueOf(nextIdS.substring(8, nextIdS.length())));
            categoryMapper.saveCategory(category);
        }
    }

    @Override
    public void deleteCategoryById(Long id) {
        categoryMapper.deleteCategoryById(id);
    }

    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }


}
